/*
 * @lc app=leetcode.cn id=59 lang=javascript
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function (n) {
  let arr = Array(n).fill(0).map(() => Array(n).fill(0));
  let i = 0, j = -1, k = 0;
  while (k < n * n) {
    while (j < n - 1 && arr[i][j + 1] === 0) arr[i][++j] = ++k;
    while (i < n - 1 && arr[i + 1][j] === 0) arr[++i][j] = ++k;
    while (j > 0 && arr[i][j - 1] === 0) arr[i][--j] = ++k;
    while (i > 0 && arr[i - 1][j] === 0) arr[--i][j] = ++k;
  }
  return arr;
};
// @lc code=end

generateMatrix(4);